public class WaitDemo2 {
    public static void main(String[] args) {
        Object lock=new Object();
        Object lock2=new Object();
        Thread t1=new Thread(()->{
            System.out.println("线程1开始执行");
            try {
                synchronized (lock){
                    System.out.println("线程1调用wait方法...");
                    //无限期的等待状态
                    lock.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程1执行完成");
        },"线程1");
        t1.start();

        Thread t2=new Thread(()->{
            System.out.println("线程2开始执行");
            try {
                synchronized (lock){
                    System.out.println("线程2调用wait方法...");
                    //无限期的等待状态
                    lock.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程2执行完成");
        },"线程2");
        t2.start();

        Thread t3=new Thread(()->{
            System.out.println("线程3开始执行");
            try {
                synchronized (lock2){
                    System.out.println("线程3调用wait方法...");
                    //无限期的等待状态
                    lock2.wait();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程3执行完成");
        },"线程3");
        t3.start();

        //唤醒lock对象上休眠的线程(随机唤醒)
        Thread t4=new Thread(()->{
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("线程4：开始执行，唤醒线程");
            synchronized (lock){
                //发出唤醒通知
                lock.notify();
                System.out.println("线程4：执行了唤醒操作");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("线程4：synchronized执行完了");
            }
        },"线程4");
        t4.start();
    }
}
